﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace Vaffanculo.Presentation.Component.WizardControl.Wizards.WizardsMDX
{
    /// <summary>
    /// Interaction logic for PagePrincipal.xaml
    /// </summary>
    public partial class PagePrincipal : System.Windows.Controls.UserControl, IPageComponent
    {
        private ConectDataBase _conectDataBasePostgres;
        public PagePrincipal()
        {
            InitializeComponent();
            _conectDataBasePostgres = new ConectDataBase();
            BtnOk.Click += BtnOkClick;
            BtnRemove.Click += BtnRemoveClick;
            FillList();
        }

        void BtnRemoveClick(object sender, RoutedEventArgs e)
        {
            List<string > listItem = new List<string>();

            foreach (var selectedItem in List2Joins.Items)
            {
                if(List2Joins.SelectedItem ==selectedItem) continue;

                listItem.Add((string)selectedItem);
            }
            List2Joins.Items.Clear();
            foreach (string item in listItem)
            {
                List2Joins.Items.Add(item);

            }
        }

        void BtnOkClick(object sender, RoutedEventArgs e)
        {
           
            if(List1Result.SelectedItem!=null &&
                List2Result.SelectedItems!=null)
            {
                /*CheckBox checkBox = new CheckBox();
                checkBox.Content = string.Format("{0}.{1} JOIN {2}.{3}", List1.SelectedItem, List1Result.SelectedItem,
                                                 List2.SelectedItem, List2Result.SelectedItem);
                List2Joins.Items.Add(checkBox);*/
                List2Joins.Items.Add(string.Format("{0}.{1} JOIN {2}.{3}", List1.SelectedItem, List1Result.SelectedItem,
                                                 List2.SelectedItem, List2Result.SelectedItem));
            }
        }

        public void FillList()
        {
            List1.SelectionChanged += List1SelectionChanged;
            List2.SelectionChanged += List2SelectionChanged;
            
            List1.Items.Add("fct_caudales_embalsado_energia_disponible");
            List1.Items.Add("dim_centrales_sistema");
            List1.Items.Add("dim_tiempo");


            List2.Items.Add("fct_caudales_embalsado_energia_disponible");
            List2.Items.Add("dim_centrales_sistema");
            List2.Items.Add("dim_tiempo");
        }

        void List1SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string tableName = List1.SelectedItems[0].ToString();
            string queryPostgres =
                string.Format(
                    "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{0}';", tableName);
            DataSet result = _conectDataBasePostgres.ExecuteQuery(queryPostgres);
            List1Result.Items.Clear();

            foreach (DataRow row in result.Tables[0].Rows)
            {
                //var radioButton = new RadioButton {Content = row["column_name"]};
                //List1Result.Items.Add(radioButton);
                List1Result.Items.Add(row["column_name"]);
            }
        }


        void List2SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string tableName = List2.SelectedItems[0].ToString();
            string queryPostgres =
                string.Format(
                    "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '{0}';", tableName);
            DataSet result = _conectDataBasePostgres.ExecuteQuery(queryPostgres);
            List2Result.Items.Clear();

            foreach (DataRow row in result.Tables[0].Rows)
            {
                //var radioButton = new RadioButton {Content = row["column_name"]};
                //List2Result.Items.Add(radioButton);
                List2Result.Items.Add(row["column_name"]);
            }

        }

        public string KeyText
        {
            get { return "Selección de Joins"; }
        }



        
    }
}
